﻿using KinonekoSoftware.CodariaDB.Documents.TemplateContents;

namespace KinonekoSoftware.Studio.Pages.Instances.Templated
{

    public abstract class TemplatedEditorBase<TTemplated, TEditor> : TemplatedEditorBase<TTemplated>
        where TTemplated : TemplatedContent
        where TEditor : TabViewModelCore
    {
        private Instance _instanceContent;
        
        protected TemplatedEditorBase()
        {
            AdvancedEditCommand = CreateCommand(DoAdvancedEditCommand);
        }


        protected abstract TEditor GetEditorCore();
        
        protected abstract void GetOrCreate(InstanceCache cache, out TTemplated content, out Instance instanceContent);

        protected override void OnLoad()
        {
            GetOrCreate(InstanceCache, out _templatedContent, out _instanceContent);
        }

        private void GotoAdvancedEditor(TabViewModel vm, InstanceCache cache)
        {
            var context = new PageContext { Id = Platform.GetAdvancedEditorID(cache.Id) };

            context.Set(ContextBase.Key_Value, cache);
            GoTo(vm, context);
        }


        private void DoAdvancedEditCommand()
        {
            GotoAdvancedEditor(GetEditorCore(), InstanceCache);
        }


        public Instance  InstanceContent     => _instanceContent;
        public ICommandEX AdvancedEditCommand { get; }
    }
}